<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Dataset_db extends CI_Model
{
	function getIssues()
	{
		$this->db->where("is_featured = 1");
		$this->db->order_by("id", "desc");
		$query = $this->db->get('issues');		
		return $query->result();		
	}
	
	function getArticles()
	{
		$this->db->where("is_featured = 1");
		$this->db->order_by("id", "desc");
		$query = $this->db->get('issues_detail');		
		return $query->result();		
	}
	
	function getRoles()
	{
		$query = $this->db->get('sec_role');
		return $query->result();		
	}

	function getTypes($datacode)
	{
		$this->db->select('data_id,data_value,data_display,description');
 		$this->db->where("data_code='$datacode' AND display='1'"); 
		$this->db->order_by("order_idx", "asc"); 
		$query = $this->db->get('sec_dataset');
		return $query->result();		
	}
	
	function getSearchTypes($datacode)
	{
		$this->db->select('data_id,data_value,data_display,description');
 		$this->db->where(array('data_code'=>$datacode)); 
		$this->db->order_by("order_idx", "asc"); 
		$query = $this->db->get('sec_dataset');
		return $query->result();		
	}
	
	function getNavs($parent)
	{
		$this->db->select('module_id,module,link,icon');
		$this->db->order_by("sort", "asc"); 
		$query = $this->db->get_where('sec_modules', array('publish' => '1','parent' => $parent));
		return $query->result_array();		
	}
	
	function buildNav($p)
	{
		$rs = $this->getNavs($p);
		$nav = array();
		foreach ($rs as $row)
			{
				$nav[] = array ('row'=>$row,'child'=>$this->buildNav($row['module_id']));			
			}				
		return $nav;
	}
	
	function getModule($module)
	{
		$this->db->select('module_id,module,link,icon');
		$query = $this->db->get_where('sec_modules', array('publish' => '1','link' => $module));
		return $query->row_array();		
	}


	
	function getData()
	{
		$this->db->select('data_id,data_code');
		$this->db->order_by("data_id", "asc"); 
		$this->db->groupby("data_code"); 
		$query = $this->db->get('sec_dataset');
		return $query->result();		
	}

	
	function saveLog($data)
	{
	
		$user = $this->session->userdata('user');
		$data['user_id'] = $user['user_id'];	
		
		$data['log_date'] = date("Y-m-d H:i:s");
		$data['machine_name'] = gethostbyaddr($_SERVER['REMOTE_ADDR']);
		$data['ip_address'] = $_SERVER['REMOTE_ADDR'];  
		
		$this->db->insert('sec_logs', $data);
	}
	
	function getList()
	{		
		$sortname = $this->input->post('sortname');
		$sortorder = $this->input->post('sortorder');
		$page = $this->input->post('page');
		$rp = $this->input->post('rp');		
		
		$query = $this->input->post('query');
		$qtype = $this->input->post('qtype');
		
		$stype = $this->input->post('s_type');
		$otype = $this->input->post('o_type');
		$skey = $this->input->post('s_key');
	
		$cond = "";

		if (!$sortname) $sortname = 'subscription_id';
		if (!$sortorder) $sortorder = 'DESC';
		
		
		if (!$page) $page = 1;
		if (!$rp) $rp = 25;
		
		$start = (($page-1) * $rp);
		
		$this->db->start_cache();
		
		if ($stype) 
		{
			foreach ($stype as $key => $value)
			{
				if ($skey[$key])
				{
					if ($stype[$key]=='date')  $match = convertDate($skey[$key]);
					else $match = $skey[$key];
					
					switch ($otype[$key])
					{
						case "=":
							$this->db->where($stype[$key],$match); 
							break;
						case "!=":
							$this->db->where($stype[$key]." !=",$match); 
							break;
						case "like":
							$this->db->like($stype[$key],$match); 
							break;
						case ">":
							$this->db->where($stype[$key]." >",$match); 
							break;
						case "<":
							$this->db->where($stype[$key]." <",$match); 
							break;
					}
				}
			}
			
		}		
		
		$this->db->select("a.subscription_id as pkey, a.subscription_id as test, a.subscription_id, a.name, a.contact, a.email, b.product_name, a.created");
		$this->db->where('a.is_deleted','0');
		$this->db->join("products b","b.product_id = a.product_id", "left");
		$this->db->order_by($sortname,$sortorder);
		$this->db->limit($rp, $start);		
		$query = $this->db->get("subscriptions a");
		$num = $this->db->count_all_results();

		$this->db->flush_cache();
		
		$data['db'] = $query;
		$data['page'] = $page;
		$data['num'] = $num;
		return $data;			
		
	}

}

?>